<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
            </div>
        </script>

        <table class="layui-hide" id="noticeTable" lay-filter="currentTableFilter"></table>

    </div>


</div>


<script type="text/html" id="barDemo">
    <input type="button" class="layui-btn layui-btn-xs" lay-event="edit" value="编辑">
    <input type="button" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" value="删除">
</script>


</body>

<script src="/lib/layui/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['layedit','form', 'table', 'jquery', ], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layedit = layui.layedit;


        table.render({
            elem: '#noticeTable'
            , id: 'noticeTable'
            // , height: 300
            // , width: 800
            , url: "/affiche/findAll.do" //数据接口
            , page: true //开启分页
            , type: "get"
            , toolbar: "#toolbarDemo"
            , request: {
                pageName: 'pageNow' //页码的参数名称，默认：page
                ,
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            // , toolbar: '#toolbarDemo'
            , limit: 2
            , limits: [1, 2, 3, 4, 5]
            , method: 'get'
            , cols: [[ //表头
                {type: 'checkbox'}
                , {field: 'title', title: '公告名称', align: 'center'}
                , {field: 'id', title: 'ID', width: '15%', align: 'center', hide: true}
                , {field: 'content', title: '公告内容', align: 'center'}
                , {field: 'publishTime', title: '创建时间', align: 'center',templet : "<div>{{layui.util.toDateString(d.publishTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
                , {field: 'publisher', title: '公告人', align: 'center'}
                , {title: '操作', align: 'center', toolbar: '#barDemo'}

            ]]
            , done: function (res, curr, count) {
                var that = this.elem.next();
                res.data.forEach(function (item, index) {
                    //item表示每列显示的数据
                    /* if (index % 2 == 0) {
                         var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", "#558ff2");
                     } else {
                         var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", "white");
                     }
*/
                })
            }
        });

        /**
         * 1.删除公告
         * 2.修改公告
         * **/
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            alert(data.id)
            if (obj.event === 'del') {
                layer.confirm('确定删除该角色111吗？', {
                    btn: ['确定', '取消'] //可以无限个按钮
                }, function (index, layero) {

                    $.ajax({
                        url: "/affiche/remove.do"
                        , dataType: "json"
                        , type: "get"
                        , data: {"id": data.id}
                        , success: function (rec) {
                            if (rec.code == 0) {
                                layer.msg("恭喜删除成功");
                                setTimeout(function () {
                                    location.href = "/html/notice/notice.html"
                                }, 2000)
                            } else {
                                layer.msg("抱歉删除失败");
                                setTimeout(function () {
                                    location.href = "/html/notice/notice.html"
                                }, 2000)
                            }
                        }
                    })
                }, function (index) {

                });

            } else if (obj.event === 'edit') {
                layer.open({
                    type: 1
                    , title: "公告修改" //不显示标题栏
                    , closeBtn: false
                    , area: ['100%','100%']
                    , shade: 0.8
                    , id: 'LAY_layuipro' //设定一个id，防止重复弹出
                    , btn: ['确认修改', '取消']
                    , btnAlign: 'c'
                    , moveType: 1 //拖拽模式，0或者1
                    , content: '<from class="layui-form layuimini-form">' +
                        '<from class="layui-form layuimini-form">\n' +
                        '    <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label required">公告标题</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '        <input type="text" name="title" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入角色编号" value="" class="layui-input">\n' +
                        '    </div>\n' +
                        '</div>\n' +
                        '    <div class="layui-form-item">\n' +
                        '        <label class="layui-form-label required">内容</label>\n' +
                        '        <div class="layui-input-block">\n' +
                        '            <input type="text" name="content" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入角色名称" value="" class="layui-input">\n' +
                        '        </div>\n' +
                        '    </div>\n' +
                        '</from>'
                    , success: function (layero) {
                      $("input[name=title]").val(data.title);
                       $("input[name=content]").val(data.content);
                    },yes: function (index, layero) {
                        var title = $("input[name=title]").val();
                        var content = $("input[name=content]").val();
                        if (title == null || title == "") {
                            layer.msg("标题不能为空")
                        } else if (content == null || content == "") {
                            layer.msg("内容不能为空")
                        }  else {
                            $.ajax({
                                url: "/affiche/update.do"
                                , dataType: "json"
                                , type: "get"
                                , data: {"title": title, "content": content,"id":data.id,"publishTime":data.publishTime}
                                , success: function (rec) {
                                    if (rec.code == 0) {
                                        layer.msg("修改成功...");
                                        setTimeout(function () {
                                            location.href = "/html/notice/notice.html"
                                        }, 2000)
                                    } else {
                                        layer.msg("修改失败");
                                        setTimeout(function () {
                                            location.href = "/html/notice/notice.html"
                                        }, 2000)
                                    }
                                }

                            })

                        }
                    }
                })
            }
        });

        /**
         * 公告添加
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            var demo = null;

          /*  layedit.build('demo'); //建立编辑器*/
            if (obj.event === 'add') {// 监听添加操作
                var index = layer.open({
                    type: 1
                    , title: "添加公告" //不显示标题栏
                    , closeBtn: false
                    , area: ['80%','80%']
                    , shade: 0.8
                    , id: 'LAY_layuipro' //设定一个id，防止重复弹出
                    , btn: ['添加', '取消']
                    , btnAlign: 'c'
                    , moveType: 1 //拖拽模式，0或者1
                    , content: '<from class="layui-form ">' +
                        '<from class="layui-form">\n' +
                        '    <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label required">公告标题</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '        <input type="text" name="title" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入公告标题" value="" class="layui-input">\n' +
                        '    </div>\n' +
                        '</div>\n' +
                        '    <div class="layui-form-item">\n' +
                        '        <label class="layui-form-label required">公告时间</label>\n' +
                        '        <div class="layui-input-block">\n' +
                        '            <input type="date" id="noticeTime" name="starttime" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入公告时间" value="" class="layui-input">\n' +
                        '        </div>\n' +
                        '    </div>\n' +
                        '<div class="layuimini-container">\n' +
                        '    <div class="layuimini-main">\n' +
                        '\n' +
                        '        <textarea id="demo" style="display: none;" name="demo"></textarea>\n' +
                        '    </div>\n' +
                        '</div>' +
                        '</from>'
                    , success: function (layero,index) {
                        demo = layedit.build('demo'); //建立编辑器
                        form.render('radio');


                    }, yes: function (index, layero) {
                        var title = $("input[name=title]").val();
                        var publishtime = $("input[name=starttime]").val();
                        var content = layedit.getContent(demo);





                        if (title == null || title == "") {
                            layer.msg("公告标题不能为空")
                        } else if (publishtime == null || publishtime == "") {
                            layer.msg("发布时间不能为空")
                        } else if (content == null || content == "") {
                            layer.msg("发布内容不能为空")
                        } else {
                            $.ajax({
                                url: "/affiche/add.do"
                                , dataType: "json"
                                , type: "get"
                                , data: {"title": title, "publishTime": publishtime, "content": content}
                                , success: function (rec) {
                                    if (rec.code == 0) {
                                        layer.msg("添加公告成功...");
                                        setTimeout(function () {
                                            location.href = "/html/notice/notice.html"
                                        }, 2000)
                                    } else {
                                        layer.msg("添加公告失败");
                                        setTimeout(function () {
                                            location.href = "/html/notice/notice.html"
                                        }, 2000)
                                    }
                                }

                            })

                        }

                    }
                })

            }
        });
    });
</script>
</html>